<?php
namespace Common\Model;
use Think\Model;
use Think\Upload;

/**
 * 文章 模型
 */
class ArticleModel extends Model {
	public static $mystat = array('del'=>'-1','forbid'=>'0','allow'=>'1');
	public static $ablemap = array('status'=>'1'); //状态正常的查询条件

	protected $_auto = array(
			array('updatetime',NOW_TIME,self::MODEL_BOTH)
	);
	
	public function myUpdate($data) {
		$id = (int)$data['id'];
		if ($id<=0) {
			$this->error = '请先选择要更新的数据';
			return false;
		}
		//先create验证数据
		if (false ===$this->create($data,self::MODEL_UPDATE)) return false;
	
		//上传门店图片
		if (!empty($data['picurl'])) {
			$setting = C('PICTURE_UPLOAD');
			$Upload = new Upload($setting);
			$image = $Upload->uploadOne($data['picurl']);
			if (!$image) {
				$this->error = $Upload->getError();
				return false;
			}
			$image['path'] = $setting['rootPath'].$image['savepath'].$image['savename']; //在模板里的url路径
			$this->picurl = $image['path'];
		}else {
			unset($this->picurl);
		}
	
		return $this->where('`id`='.$id)->save();
	}
	/**
	 * 新增 文章
	 * @param array $data
	 * @return boolean
	 */
	public function myAdd($data) {
		//先create验证数据
		if (false ===$this->create($data,self::MODEL_INSERT)) return false;
	
		//上传商品图片
		if (!empty($data['picurl'])) {
			$setting = C('PICTURE_UPLOAD');
			$Upload = new Upload($setting);
			$image = $Upload->uploadOne($data['picurl']);
			if (!$image) {
				$this->error = $Upload->getError();
				return false;
			}
			$image['path'] = $setting['rootPath'].$image['savepath'].$image['savename']; //在模板里的url路径
			$this->picurl = $image['path'];
		}else {
			unset($this->picurl);
		}
		return $this->add();
	}
}
